package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.ServicePackBo;
import com.ruoyi.gp.domain.vo.ServicePackVo;
import com.ruoyi.gp.domain.ServicePack;
import com.ruoyi.gp.mapper.ServicePackMapper;
import com.ruoyi.gp.service.IServicePackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 服务包Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-07
 */
@RequiredArgsConstructor
@Service
public class ServicePackServiceImpl implements IServicePackService {

    private final ServicePackMapper baseMapper;

    /**
     * 查询服务包
     */
    @Override
    public ServicePackVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务包列表
     */
    @Override
    public TableDataInfo<ServicePackVo> queryPageList(ServicePackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServicePack> lqw = buildQueryWrapper(bo);
        Page<ServicePackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务包列表
     */
    @Override
    public List<ServicePackVo> queryList(ServicePackBo bo) {
        LambdaQueryWrapper<ServicePack> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServicePack> buildQueryWrapper(ServicePackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ServicePack> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getServiceName()), ServicePack::getServiceName, bo.getServiceName());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceImage()), ServicePack::getServiceImage, bo.getServiceImage());
        lqw.eq(bo.getServicePrice() != null, ServicePack::getServicePrice, bo.getServicePrice());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceText()), ServicePack::getServiceText, bo.getServiceText());
        lqw.eq(bo.getShoppingPlatformId() != null, ServicePack::getShoppingPlatformId, bo.getShoppingPlatformId());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), ServicePack::getUrl, bo.getUrl());
        lqw.eq(bo.getServiceCategory() != null, ServicePack::getServiceCategory, bo.getServiceCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceStart()), ServicePack::getServiceStart, bo.getServiceStart());
        lqw.eq(bo.getServiceIncome() != null, ServicePack::getServiceIncome, bo.getServiceIncome());
        return lqw;
    }

    /**
     * 新增服务包
     */
    @Override
    public Boolean insertByBo(ServicePackBo bo) {
        ServicePack add = BeanUtil.toBean(bo, ServicePack.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务包
     */
    @Override
    public Boolean updateByBo(ServicePackBo bo) {
        ServicePack update = BeanUtil.toBean(bo, ServicePack.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ServicePack entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除服务包
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
